package web.demo.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import web.demo.dao.IOrderDetailDao;
import web.demo.entity.FoodDetail;
import web.demo.entity.OrderDetail;
import web.demo.utils.JdbcUtils;

public class OrderDetailDao implements IOrderDetailDao{

	@Override
	public boolean addOrderDetail(OrderDetail orderDetail) {
		String sql="insert into orderDetail (food_id,orders_id,food_count) values(?,?,?)";
		Object[] params={
				orderDetail.getFood_id(),
				orderDetail.getOrders_id(),
				orderDetail.getFood_count()
		};
		try{
			int result=JdbcUtils.getQueryRunner().update(sql,params);
			if(result>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<OrderDetail> getAll() {
		//查询应该是多表查询，获取得到多个表的数据
		StringBuilder sb=new StringBuilder();
		sb.append(" select ");
		sb.append("	  od.id,");
		sb.append("   f.name as food_name,");
		sb.append("   f.price as food_price,");
		sb.append("   od.food_count,");
		sb.append("   (od.food_count*f.price) as food_countMoney ");
		sb.append(" from ");
		sb.append("   orderDetail od ");
		sb.append("   inner join orders o ");
		sb.append("   inner join food f ");
		sb.append(" where ");
		sb.append("   1=1 ");
		sb.append("   and od.orders_id=o.id ");
		sb.append("   and od.food_id=f.id ");
		sb.append(" order by od.id asc ");
		try{
			return JdbcUtils.getQueryRunner().query(sb.toString(),new BeanListHandler<OrderDetail>(OrderDetail.class));
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}

	@Override
	public void update(OrderDetail orderDetail) {
		String sql="update orderDetail set food_id=?,orders_id=?,food_count=? where id=? ";
		Object[] params={
				orderDetail.getFood_id(),
				orderDetail.getOrders_id(),
				orderDetail.getFood_count(),
				orderDetail.getId()
		};
		try {
			JdbcUtils.getQueryRunner().update(sql,params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	@Override
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<OrderDetail> getOrderDetail(int ordersId) {
		//查询应该是多表查询，获取得到多个表的数据
		StringBuilder sb=new StringBuilder();
		sb.append(" select ");
		sb.append("	  od.id,");
		sb.append("	  f.id as food_id ,");
		sb.append("   f.name as food_name,");
		sb.append("   f.price as food_price,");
		sb.append("	  o.id as orders_id,");
		sb.append("   od.food_count,");
		sb.append("   (od.food_count*f.price) as food_countMoney ");
		sb.append(" from ");
		sb.append("   orderDetail od ");
		sb.append("   inner join orders o ");
		sb.append("   inner join food f ");
		sb.append(" where ");
		sb.append("   1=1 ");
		sb.append("   and od.orders_id=o.id ");
		sb.append("   and od.food_id=f.id ");
		sb.append("   and o.id=? ");
		sb.append(" order by od.id asc ");
		try {
			return JdbcUtils.getQueryRunner().query(sb.toString(),new BeanListHandler<OrderDetail>(OrderDetail.class),ordersId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
